package com.dlc.shop.platform.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dlc.shop.allinpay.service.AllinpayCompanyService;
import com.dlc.shop.bean.app.param.ShopAuditingParam;
import com.dlc.shop.bean.bo.PayInfoResultBO;
import com.dlc.shop.bean.dto.ShopAuditingInfoDto;
import com.dlc.shop.bean.enums.ShopStatus;
import com.dlc.shop.bean.model.PayInfo;
import com.dlc.shop.bean.model.RefundInfo;
import com.dlc.shop.bean.model.ShopAuditing;
import com.dlc.shop.bean.model.ShopDetail;
import com.dlc.shop.bean.param.AuditingInfoParam;
import com.dlc.shop.bean.param.ShopTypeParam;
import com.dlc.shop.common.annotation.SysLog;
import com.dlc.shop.common.exception.YamiShopBindException;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.common.util.PasswordUtil;
import com.dlc.shop.common.util.RedisUtil;
import com.dlc.shop.jky.common.foreign.service.ForeignShopService;
import com.dlc.shop.security.common.manager.PasswordManager;
import com.dlc.shop.security.platform.util.SecurityUtils;
import com.dlc.shop.service.HuifuService;
import com.dlc.shop.service.ShopAuditingService;
import com.dlc.shop.service.ShopDetailService;
import com.dlc.shop.sys.common.model.ShopEmployee;
import com.dlc.shop.sys.common.service.ShopEmployeeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;


/**
 * 商家审核信息
 *
 * @author Dwl
 * @date 2019-09-19 14:02:57
 */
@RestController
@AllArgsConstructor
@RequestMapping("/shop/huifu")
@Tag(name = "汇付天下账户")
@Slf4j
public class ShopHuifuController {

    private final ShopDetailService shopDetailService;
    private final HuifuService huifuService;

    @GetMapping("/open/{shopId}")
    @Parameter(name = "shopId", description = "店铺id")
    public ServerResponseEntity<ShopDetail> open(@PathVariable Long shopId) throws Exception{
        // 获取店铺信息
        ShopDetail shopDetail = shopDetailService.getById(shopId);
        Map<String, Object> huifuRes = huifuService.huifuOpen(shopDetail);
        try {
            if(huifuRes.get("resp_code").equals("00000000")) {
                shopDetail.setHuifuId(huifuRes.get("huifu_id").toString());
                shopDetail.setLoginName(huifuRes.get("login_name").toString());
                shopDetail.setLoginPassword(huifuRes.get("login_password").toString());
                shopDetailService.updateById(shopDetail);
            } else {
                return ServerResponseEntity.showFailMsg(huifuRes.get("resp_desc").toString());
            }
        } catch (NullPointerException e) {
            return ServerResponseEntity.showFailMsg(huifuRes.get("resp_desc").toString());
        }
        return ServerResponseEntity.success();
    }


    @GetMapping("/bind/{shopId}")
    @Parameter(name = "shopId", description = "店铺id")
    public ServerResponseEntity<String> bind(@PathVariable Long shopId) throws Exception{
        // 获取店铺信息
        ShopDetail shopDetail = shopDetailService.getById(shopId);
        if(shopDetail.getHuifuId() == null || shopDetail.getHuifuId().equals("")) {
            return ServerResponseEntity.showFailMsg("请先进行开户申请");
        }
        Map<String, Object> huifuRes = huifuService.huifuBind(shopDetail);

        if(huifuRes.get("resp_code").equals("00000000")) {
            shopDetail.setIsBind(1);
            shopDetailService.updateById(shopDetail);
            return ServerResponseEntity.success(huifuRes.get("resp_desc").toString());
        }
        return ServerResponseEntity.showFailMsg(huifuRes.get("resp_desc").toString());
    }

    @PostMapping("/callback")
    public String huiFuCallback(HttpServletRequest request){
        String respCode = request.getParameter("resp_code");;
        JSONObject respData = JSONObject.parseObject(request.getParameter("resp_data"));
        log.info("汇付绑定用户回调-------------------------："+respData.toString());

        if(respCode.equals("00000000")){

            String huifuId = respData.getString("huifu_id");
            ShopDetail shopDetail = shopDetailService.getOne(new LambdaQueryWrapper<ShopDetail>().eq(ShopDetail::getHuifuId, huifuId));
            shopDetail.setIsBind(1);
            shopDetailService.updateById(shopDetail);
            return "RECV_ORD_ID_" + respData.get("req_seq_id").toString();
        }
        return "";
    }



}
